package Controlador;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
public class ConnectionDB{
    
    private Connection connection;
    private String Path ="/";
    public ConnectionDB() {
        URL url = this.getClass().getResource("");
        String[] v = url.getPath().split("/");
        for (int i = 0; i < v.length-3; i++) {
            Path+=v[i]+"/";
        }
    }
    
    
    public boolean Connect(){
        try {
            Class.forName(Configuration.getDataBaseDriver(Path));
            this.connection = DriverManager.getConnection(Configuration.getDataBaseURL(Path), Configuration.getDataBaseUser(Path), Configuration.getDataBasePassword(Path));
            this.connection.setAutoCommit(false);
            System.out.println("Se establecio conexion con la base de datos");
            return(true);
        }
        catch (SQLException ex){
            System.out.println("No se pudo iniciar conexion con DB [" + ex.getMessage() + "]");
            return(false);
        }
        catch (ClassNotFoundException ex){
            System.out.println("No se puede cargar driver de conexion con DB [" + ex.getMessage() + "]");
            return(false);
        }
        catch (IOException ex){ 
            System.out.println("Documento de XML no accesible [" + ex.getMessage() + "]");
            return(false);
        }
        catch (Exception ex){
            System.out.println("La configuracion no es correcta [" + ex.getMessage() + "]");
            return(false);
        }
    }
    
    public int getUpdate(String Query){
        try {
            Statement statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println("Actualizar DB [" + Query + "]");
            return(statement.executeUpdate(Query));
        } 
        catch (SQLException ex){
            System.out.println("No se puede ejecutar actualizacion DB [" + ex.getMessage() + "]");
            return(0); 
        }
    }
    
    public ResultSet getQuery(String Query){
        try {
            Statement statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println("Consulta DB [" + Query + "]");
            return(statement.executeQuery(Query));
        } 
        catch (SQLException ex){
            System.out.println("No se puede ejecutar actualizacion DB [" + ex.getMessage() + "]");
            return(null);
        }
    }
    
    public int getNumRows(ResultSet resultSet){
        try{
            resultSet.last();
            int rows = resultSet.getRow();
            resultSet.beforeFirst();
            return(rows);
        }
        catch(SQLException ex){
            System.out.println("No se puede contar el numero de filas [" + ex.getMessage() + "]");
            return(0);
        }
    }
    
    public void close(){
        try{
            this.connection.close();
            System.out.println("Se cerro conexion con la base de datos");
        }
        catch(Exception ex){
            System.out.println("No se pudo cerrar correctamente la conexion [" + ex.getMessage() + "]");
        }
    }
    
    public void commit(boolean commit){
        try {
            if (commit) {
                this.connection.commit();
                System.out.println("Se realizó commit correctamente");
            } else {
                this.connection.rollback();
                System.out.println("Se realizó rollback correctamente");
            }
        } catch (Exception e) {
            System.out.println("la operación commit/rollback no se pudo realizar");
        }
    }
    
}